﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Travel.DAL
{
	/// <summary>
	/// 数据访问类:route
	/// </summary>
	public partial class route
	{
		public route()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("id", "route"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from route");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Travel.Model.route model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into route(");
			strSql.Append("name,type,key_words,start_with,end_with,start_id,end_id,days_account,begin_date,created_on,created_by,changed_on,changed_by)");
			strSql.Append(" values (");
			strSql.Append("@name,@type,@key_words,@start_with,@end_with,@start_id,@end_id,@days_account,@begin_date,@created_on,@created_by,@changed_on,@changed_by)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@name", SqlDbType.NVarChar,500),
					new SqlParameter("@type", SqlDbType.Int,4),
					new SqlParameter("@key_words", SqlDbType.NVarChar,500),
					new SqlParameter("@start_with", SqlDbType.NVarChar,500),
					new SqlParameter("@end_with", SqlDbType.NVarChar,500),
					new SqlParameter("@start_id", SqlDbType.Int,4),
					new SqlParameter("@end_id", SqlDbType.Int,4),
					new SqlParameter("@days_account", SqlDbType.Int,4),
					new SqlParameter("@begin_date", SqlDbType.Date,3),
					new SqlParameter("@created_on", SqlDbType.DateTime),
					new SqlParameter("@created_by", SqlDbType.NVarChar,50),
					new SqlParameter("@changed_on", SqlDbType.DateTime),
					new SqlParameter("@changed_by", SqlDbType.NVarChar,50)};
			parameters[0].Value = model.name;
			parameters[1].Value = model.type;
			parameters[2].Value = model.key_words;
			parameters[3].Value = model.start_with;
			parameters[4].Value = model.end_with;
			parameters[5].Value = model.start_id;
			parameters[6].Value = model.end_id;
			parameters[7].Value = model.days_account;
			parameters[8].Value = model.begin_date;
			parameters[9].Value = model.created_on;
			parameters[10].Value = model.created_by;
			parameters[11].Value = model.changed_on;
			parameters[12].Value = model.changed_by;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Travel.Model.route model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update route set ");
			strSql.Append("name=@name,");
			strSql.Append("type=@type,");
			strSql.Append("key_words=@key_words,");
			strSql.Append("start_with=@start_with,");
			strSql.Append("end_with=@end_with,");
			strSql.Append("start_id=@start_id,");
			strSql.Append("end_id=@end_id,");
			strSql.Append("days_account=@days_account,");
			strSql.Append("begin_date=@begin_date,");
			strSql.Append("created_on=@created_on,");
			strSql.Append("created_by=@created_by,");
			strSql.Append("changed_on=@changed_on,");
			strSql.Append("changed_by=@changed_by");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@name", SqlDbType.NVarChar,500),
					new SqlParameter("@type", SqlDbType.Int,4),
					new SqlParameter("@key_words", SqlDbType.NVarChar,500),
					new SqlParameter("@start_with", SqlDbType.NVarChar,500),
					new SqlParameter("@end_with", SqlDbType.NVarChar,500),
					new SqlParameter("@start_id", SqlDbType.Int,4),
					new SqlParameter("@end_id", SqlDbType.Int,4),
					new SqlParameter("@days_account", SqlDbType.Int,4),
					new SqlParameter("@begin_date", SqlDbType.Date,3),
					new SqlParameter("@created_on", SqlDbType.DateTime),
					new SqlParameter("@created_by", SqlDbType.NVarChar,50),
					new SqlParameter("@changed_on", SqlDbType.DateTime),
					new SqlParameter("@changed_by", SqlDbType.NVarChar,50),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.name;
			parameters[1].Value = model.type;
			parameters[2].Value = model.key_words;
			parameters[3].Value = model.start_with;
			parameters[4].Value = model.end_with;
			parameters[5].Value = model.start_id;
			parameters[6].Value = model.end_id;
			parameters[7].Value = model.days_account;
			parameters[8].Value = model.begin_date;
			parameters[9].Value = model.created_on;
			parameters[10].Value = model.created_by;
			parameters[11].Value = model.changed_on;
			parameters[12].Value = model.changed_by;
			parameters[13].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from route ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from route ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Travel.Model.route GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,name,type,key_words,start_with,end_with,start_id,end_id,days_account,begin_date,created_on,created_by,changed_on,changed_by from route ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			Travel.Model.route model=new Travel.Model.route();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["name"]!=null && ds.Tables[0].Rows[0]["name"].ToString()!="")
				{
					model.name=ds.Tables[0].Rows[0]["name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["type"]!=null && ds.Tables[0].Rows[0]["type"].ToString()!="")
				{
					model.type=int.Parse(ds.Tables[0].Rows[0]["type"].ToString());
				}
				if(ds.Tables[0].Rows[0]["key_words"]!=null && ds.Tables[0].Rows[0]["key_words"].ToString()!="")
				{
					model.key_words=ds.Tables[0].Rows[0]["key_words"].ToString();
				}
				if(ds.Tables[0].Rows[0]["start_with"]!=null && ds.Tables[0].Rows[0]["start_with"].ToString()!="")
				{
					model.start_with=ds.Tables[0].Rows[0]["start_with"].ToString();
				}
				if(ds.Tables[0].Rows[0]["end_with"]!=null && ds.Tables[0].Rows[0]["end_with"].ToString()!="")
				{
					model.end_with=ds.Tables[0].Rows[0]["end_with"].ToString();
				}
				if(ds.Tables[0].Rows[0]["start_id"]!=null && ds.Tables[0].Rows[0]["start_id"].ToString()!="")
				{
					model.start_id=int.Parse(ds.Tables[0].Rows[0]["start_id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["end_id"]!=null && ds.Tables[0].Rows[0]["end_id"].ToString()!="")
				{
					model.end_id=int.Parse(ds.Tables[0].Rows[0]["end_id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["days_account"]!=null && ds.Tables[0].Rows[0]["days_account"].ToString()!="")
				{
					model.days_account=int.Parse(ds.Tables[0].Rows[0]["days_account"].ToString());
				}
				if(ds.Tables[0].Rows[0]["begin_date"]!=null && ds.Tables[0].Rows[0]["begin_date"].ToString()!="")
				{
					model.begin_date=DateTime.Parse(ds.Tables[0].Rows[0]["begin_date"].ToString());
				}
				if(ds.Tables[0].Rows[0]["created_on"]!=null && ds.Tables[0].Rows[0]["created_on"].ToString()!="")
				{
					model.created_on=DateTime.Parse(ds.Tables[0].Rows[0]["created_on"].ToString());
				}
				if(ds.Tables[0].Rows[0]["created_by"]!=null && ds.Tables[0].Rows[0]["created_by"].ToString()!="")
				{
					model.created_by=ds.Tables[0].Rows[0]["created_by"].ToString();
				}
				if(ds.Tables[0].Rows[0]["changed_on"]!=null && ds.Tables[0].Rows[0]["changed_on"].ToString()!="")
				{
					model.changed_on=DateTime.Parse(ds.Tables[0].Rows[0]["changed_on"].ToString());
				}
				if(ds.Tables[0].Rows[0]["changed_by"]!=null && ds.Tables[0].Rows[0]["changed_by"].ToString()!="")
				{
					model.changed_by=ds.Tables[0].Rows[0]["changed_by"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,name,type,key_words,start_with,end_with,start_id,end_id,days_account,begin_date,created_on,created_by,changed_on,changed_by ");
			strSql.Append(" FROM route ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,name,type,key_words,start_with,end_with,start_id,end_id,days_account,begin_date,created_on,created_by,changed_on,changed_by ");
			strSql.Append(" FROM route ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM route ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from route T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "route";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

